#ifndef HNETDEVICEPRIVATE_H
#define HNETDEVICEPRIVATE_H

#include "sniffer/hnetdevice.h"

#include <QObject>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pcap.h>
#define PCAP_BUF_SIZE 					1024
#define PCAP_SRC_IF_STRING 				"rpcap://"
#define PCAP_OPENFLAG_PROMISCUOUS		1
#define PCAP_SRC_FILE					2
#define PCAP_OPENFLAG_NOCAPTURE_LOCAL	8
#define IPTOSBUFFERS    12


class HNetDevicePrivate : public QObjectData
{
    Q_DECLARE_PUBLIC(HNetDevice)
public:
    explicit HNetDevicePrivate();
    ~HNetDevicePrivate();

signals:

public slots:

protected:
    void freeAllDevs();
    bool findAllDevs();

    bool openDevice(char *szDevName, int flag, int iLengthLimit);
    bool openDevice(int devIndex, int flag, int iLengthLimit);
    bool closeDevice();

    //!
    //! \brief setDeviceFilter
    //! \param szFilter
    //! \return
    //!
    bool setDeviceFilter(const char *szFilter);

    //!
    //! \brief openDumpFile
    //! 打开堆文件（文件保存数据包）
    //! 捕获数据存储到文件
    //! \param fileName
    //! \return
    //!
    bool openDumpFile(const QString& fileName);
    void closeDumpFile();
    int  captureOnce();
    bool captureByCallBack(pcap_handler func);
    void saveCaptureData(u_char *dumpfile, struct pcap_pkthdr *header, u_char *pkt_data);
    void saveCaptureData();



    // debug
    QString ipToString(quint32 ipaddr);
    quint32 stringToIp(const QString& ipaddr_str);

    //!
    //! \brief ip6ToString ipv6的地址还不是很懂，这个转换从网上粘贴的
    //! \param sockaddr
    //! \param address
    //! \param addrlen
    //! \return
    //!
    QString ip6ToString(struct sockaddr *sockaddr, char *address, quint32 addrlen);


    void printDevsInfo();

protected:
    struct	pcap_pkthdr *m_header;
    const   u_char		*m_pktData;

    pcap_if_t		*m_allNetDevice{nullptr};					// 网络设备列表信息链表指针
    char             m_errBuffer[PCAP_ERRBUF_SIZE];				// 错误信息
    int              m_deviceCount;

    pcap_t			*m_currentDevice{nullptr};						// 当前打开的设备句柄（指针）

    pcap_dumper_t	*m_dumpFile{nullptr};						// 要保存到的文件指针


private:

};

#endif // HNETDEVICEPRIVATE_H0
